﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MSBTC.Model;

namespace MSBTC.WinForms
{
    public partial class MaterialForm : Form
    {
        private string Datamanip;

        MDIParent mdipar = new MDIParent();
        PMSContext dbcontext = new PMSContext();
        Material mtrial = new Material();

        public MaterialForm()
        {
            InitializeComponent();
        }

        private void MaterialForm_Load(object sender, EventArgs e)
        {
            txtDCost.Enabled = false;
            txtTotCost.Enabled = false;
            txtValue.Enabled = false;
            txtVAT.Enabled = false;
            txtTotInCost.Enabled = false;
            RefreshFields();
            RefreshDB();
            var prj = (from pj in dbcontext.Projects
                       select pj).ToList();
            if (prj.Count == 0)
            {
                MessageBox.Show("Cannot retrieve records from Project_TB", "System Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                foreach (var pj in prj)
                {
                    cmbPname.Items.Add(pj.Project_Name.ToString());
                }
            }

            var cat = (from c in dbcontext.Categories
                       select c).ToList();
            if (cat.Count == 0)
            {
                MessageBox.Show("Cannot retrieve records from Category_TB", "System Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                foreach (var ct in cat)
                {
                    cmbPname.Items.Add(ct.Category_Name.ToString());
                }
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();

        }

        #region functions
        private void RefreshDB()
        {
            var mat = (from c in dbcontext.Materials
                       select c).ToList();

            LVMatInfo.Items.Clear();
            if (mat != null)
            {

                foreach (var sqlMat in mat)
                {
                    
                    ListViewItem lvitems = LVMatInfo.Items.Add(sqlMat.Materialsid_PK.ToString());
                    lvitems.SubItems.Add(sqlMat.MaterialName.ToString());
                    var prj = ( from pj in dbcontext.Projects
                                where pj.Projectid_PK == sqlMat.Projectid_FK
                                select pj).SingleOrDefault();
                    if(prj != null)
                    {
                        lvitems.SubItems.Add(prj.Project_Name.ToString());
                    }

                    var cat = ( from ct in dbcontext.Categories
                                where ct.Categoryid_PK == sqlMat.Categoryid_FK
                                select ct).SingleOrDefault();
                    if(cat != null)
                    {
                        lvitems.SubItems.Add(cat.Category_Name.ToString());
                    }
                    lvitems.SubItems.Add(sqlMat.Unit.ToString());
                    lvitems.SubItems.Add(sqlMat.Quantity.ToString());
                    lvitems.SubItems.Add(sqlMat.Direct_Cost.ToString());
                    lvitems.SubItems.Add(sqlMat.TotMarkup_pct.ToString());
                    lvitems.SubItems.Add(sqlMat.TotMarkup_Value.ToString());
                    lvitems.SubItems.Add(sqlMat.Markupin_OCM.ToString());
                    lvitems.SubItems.Add(sqlMat.Markupin_Profit.ToString());
                    lvitems.SubItems.Add(sqlMat.VAT.ToString());
                    lvitems.SubItems.Add(sqlMat.TotIndCost.ToString());
                    lvitems.SubItems.Add(sqlMat.Total_Cost.ToString());
                    lvitems.SubItems.Add(sqlMat.Unit_Cost.ToString());
                }
            }
            this.Refresh();
        }

        private void ClearFields()
        {
            txtID.Clear();
            txtMName.Clear();
            cmbPname.SelectedIndex = 0;
            cmbCategory.SelectedIndex = 0;
            cmbUnit.SelectedIndex = 0;
            txtQty.Clear();
            txtDCost.Clear();
            txtVAT.Clear();
            txtPct.Clear();
            txtValue.Clear();
            txtOCM.Clear();
            txtProfit.Clear();
            txtTotCost.Clear();
            txtTotInCost.Clear();
            txtUCost.Clear();
            txtSearch.Clear();

        }

        private void DisableFields()
        {
            txtID.Enabled = false;
            txtMName.Enabled = false;
            cmbPname.Enabled = false;
            cmbCategory.Enabled = false;
            cmbUnit.Enabled = false;
            txtQty.Enabled = false;
            txtDCost.Enabled = false;
            txtVAT.Enabled = false;
            txtPct.Enabled = false;
            txtValue.Enabled = false;
            txtOCM.Enabled = false;
            txtProfit.Enabled = false;
            txtTotCost.Enabled = false;
            txtTotInCost.Enabled = false;
            txtUCost.Enabled = false;

        }

        private void EnableFields()
        {
            txtID.Enabled = true;
            txtMName.Enabled = true;
            cmbPname.Enabled = true;
            cmbCategory.Enabled = true;
            cmbUnit.Enabled = true;
            txtQty.Enabled = true;
            txtDCost.Enabled = true;
            txtVAT.Enabled = true;
            txtPct.Enabled = true;
            txtValue.Enabled = true;
            txtOCM.Enabled = true;
            txtProfit.Enabled = true;
            txtTotCost.Enabled = true;
            txtTotInCost.Enabled = true;
            txtUCost.Enabled = true;

        }

        private void RefreshFields()
        {
            DisableFields();
            ClearFields();
            RefreshDB();

            btnNew.Enabled = true;
            btnDel.Enabled = false;
            btnEdit.Enabled = false;
            btnClear.Enabled = true;
            btnSave.Enabled = false;
        }
        #endregion

        private void btnClear_Click(object sender, EventArgs e)
        {
            ClearFields();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            Datamanip = "Add";

            EnableFields();
            ClearFields();

            btnSave.Enabled = true;
            btnNew.Enabled = false;
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            Datamanip = "Edit";

            EnableFields();

            btnSave.Enabled = true;
            btnNew.Enabled = false;
            btnDel.Enabled = false;
            btnEdit.Enabled = false;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
     

            if ((txtMName.Text == "") || (txtQty.Text == "") || (txtUCost.Text == "") || (txtPct.Text == "") || (txtPct.Text == "") || (txtOCM.Text == "") || (txtProfit.Text == ""))
            {
                MessageBox.Show("Some fields were not filled up", "Data Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else if ((cmbPname.SelectedIndex == 0) || (cmbCategory.SelectedIndex == 0) || (cmbUnit.SelectedIndex == 0))
            {
                MessageBox.Show("Please Select one in Project, Category and Unit of measure", "Data Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {

                if (Datamanip == "Add")
                {
                    var prj = ( from pj in dbcontext.Projects
                                where pj.Project_Name == cmbPname.SelectedText
                                select pj).SingleOrDefault();
                    if(prj == null)
                    {
                        MessageBox.Show("Cannot retrieve Projectid_PK", "System Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                    var cat = ( from ct in dbcontext.Categories
                                where ct.Category_Name == cmbCategory.SelectedText
                                select ct).SingleOrDefault();
                    if(cat == null)
                    {
                        MessageBox.Show("Cannot retrieve Categoryid_PK", "System Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                    var mat = ( from m in dbcontext.Materials
                                where m.MaterialName == txtMName.Text && m.Projectid_FK == prj.Projectid_PK
                                && m.Categoryid_FK == cat.Categoryid_PK
                                select m).SingleOrDefault();

                    if( mat == null)
                    {
                        mtrial.MaterialName = txtMName.Text;
                        mtrial.Projectid_FK = prj.Projectid_PK;
                        mtrial.Categoryid_FK = cat.Categoryid_PK;
                        mtrial.Unit = cmbUnit.SelectedText;
                        mtrial.Quantity = Convert.ToInt32(txtQty.Text);
                        mtrial.VAT = Convert.ToInt32(txtVAT.Text);
                        mtrial.Unit_Cost = Convert.ToInt32(txtUCost.Text);
                        mtrial.TotIndCost = Convert.ToInt32(txtTotInCost.Text);
                        mtrial.Total_Cost = Convert.ToInt32(txtTotCost.Text);
                        mtrial.TotMarkup_pct = Convert.ToInt32(txtPct.Text);
                        mtrial.TotMarkup_Value = Convert.ToInt32(txtValue.Text);
                        mtrial.Markupin_OCM = Convert.ToInt32(txtOCM.Text);
                        mtrial.Markupin_Profit = Convert.ToInt32(txtProfit.Text);

                        dbcontext.Entry(mtrial).State = EntityState.Added;
                        dbcontext.SaveChanges();

                        MessageBox.Show("Successfully Saved.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        RefreshFields();
                        RefreshDB();
                    }
                }
                
            }
        }
       

    }
}
